﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyBanHang.QuerySQL;

namespace QuanLyBanHang
{
    public partial class frqlHoadonbh : Office2007Form
    {
        ClassQLHDB qlbh = new ClassQLHDB();
        private int dong;
        public frqlHoadonbh()
        {
            InitializeComponent();
        }

        private void frqlHoadonbh_Load(object sender, EventArgs e)
        {
            dtgv_HD.DataSource = qlbh.ShowHD();
            dtgv_CTHD.DataSource = qlbh.ShowCTHD(0);
        }

        private void dtgv_HD_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            dtgv_CTHD.DataSource = qlbh.TKCT(dtgv_HD.Rows[dong].Cells["clhd_mahd"].Value.ToString());
        }

        private void txtmahd_TextChanged(object sender, EventArgs e)
        {
            dtgv_HD.DataSource = qlbh.TKHD(0,txtmahd.Text,txtmakh.Text);
        }

        private void txtmakh_TextChanged(object sender, EventArgs e)
        {
            dtgv_HD.DataSource = qlbh.TKHD(1, txtmahd.Text, txtmakh.Text);
        }

        private void btnTK_Click(object sender, EventArgs e)
        {
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Bạn có chắc muốn xóa Hóa đơn " + dtgv_HD.Rows[dong].Cells["clhd_mahd"].Value.ToString() + "  ? ", "Thông báo", MessageBoxButtons.YesNo);
            if (kq == DialogResult.Yes)
            {

                qlbh.XoaCTHD(dtgv_HD.Rows[dong].Cells["clhd_mahd"].Value.ToString());
                qlbh.XoaHD(dtgv_HD.Rows[dong].Cells["clhd_mahd"].Value.ToString());
                frqlHoadonbh_Load(sender, e);
            }
        }

        private void dtgv_HD_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_HD.RowCount; i++)
                {
                    dtgv_HD.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch { }
        }

        private void dtgv_CTHD_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_CTHD.RowCount; i++)
                {
                    dtgv_CTHD.Rows[i].Cells[0].Value = i + 1;
                }
            }
            catch { }
        }

        private void btnhuy2_Click(object sender, EventArgs e)
        {
            frqlHoadonbh_Load(sender, e);
            txtmahd.Clear();
            txtmakh.Clear();
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {
            dtgv_CTHD.DataSource = qlbh.ShowCTHD(1,txtctmahd.Text);
        }
    }
}
